package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.SysOrgGroupVsDept;
import com.zp.system.entity.bo.SysOrgGroupVsDeptBo;
import com.zp.system.entity.vo.SysOrgGroupVsDeptVo;
import com.zp.system.mapper.SysOrgGroupVsDeptMapper;
import com.zp.system.service.ISysOrgGroupVsDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 科室分组与科室对照Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-18
 */
@RequiredArgsConstructor
@Service
public class SysOrgGroupVsDeptServiceImpl implements ISysOrgGroupVsDeptService {

    private final SysOrgGroupVsDeptMapper baseMapper;

    /**
     * 查询科室分组与科室对照
     */
    @Override
    public SysOrgGroupVsDeptVo queryById (String deptGroupId) {
        return baseMapper.selectVoById(deptGroupId);
    }

    /**
     * 查询科室分组与科室对照列表(分页)
     */
    @Override
    public TableDataInfo<SysOrgGroupVsDeptVo> queryPageList (SysOrgGroupVsDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOrgGroupVsDept> lqw = buildQueryWrapper(bo);
        Page<SysOrgGroupVsDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询科室分组与科室对照列表(不分页)
     */
    @Override
    public List<SysOrgGroupVsDeptVo> queryList (SysOrgGroupVsDeptBo bo) {
        LambdaQueryWrapper<SysOrgGroupVsDept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOrgGroupVsDept> buildQueryWrapper (SysOrgGroupVsDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOrgGroupVsDept> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptGroupId()), SysOrgGroupVsDept::getDeptGroupId, bo.getDeptGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), SysOrgGroupVsDept::getDeptId, bo.getDeptId());
        return lqw;
    }

    /**
     * 新增科室分组与科室对照
     */
    @Override
    public Boolean insertByBo (SysOrgGroupVsDeptBo bo) {
        SysOrgGroupVsDept add = BeanUtil.toBean(bo, SysOrgGroupVsDept.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeptGroupId(add.getDeptGroupId());
        }
        return flag;
    }

    /**
     * 修改科室分组与科室对照
     */
    @Override
    public Boolean updateByBo (SysOrgGroupVsDeptBo bo) {
        SysOrgGroupVsDept update = BeanUtil.toBean(bo, SysOrgGroupVsDept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysOrgGroupVsDept entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除科室分组与科室对照
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
